cd "/Users/mmoral/Documents/The Story of Electoral Alliances/"
clear all
* version 16				/* This line should be uncommented for Stata versions newer than 16 */

/* See the Excel file for the calculations of the party vote shares in the new districts */
import excel "All Elections.xlsx", sheet("June 2018") firstrow clear
save "June2018.dta", replace	

* 2018 June Elections 
use "June2018.dta", clear

gen old=1					/* Ankara 2 districts, Bursa a single district */
replace old=0 if _n==6 | _n>=9 & _n<=11 | _n>=22 & _n<=23
gen new=1					/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

rename (AKPARTİ MHP HDP CHP SP IYI Other) (party#), addnumber
reshape long party, i(district) j(par)  
egen dist=group(district)

** Without Alliances
** With Threshold
* With threshold & 550 MPs (Scenario 7)
v2seats party if old==1, party(par) formula(dhondt) district(dist) athres(10) size(Total_MP)
disp (295/600)	-(296/550)	/* AKP */
disp (49/600)	-(49/550)	/* MHP */
disp (67/600)	-(71/550)	/* HDP */
disp (146/600)	-(134/550)	/* CHP */
disp (43/600)	-0			/* IYI */

* With threshold & 600 MPs (Scenario 3) 
v2seats party if new==1, party(par) formula(dhondt) district(dist) athres(10) size(New_MP)
disp (295/600)	-(322/600)	/* AKP */
disp (49/600)	-(58/600)	/* MHP */
disp (67/600)	-(75/600)	/* HDP */
disp (146/600)	-(145/600)	/* CHP */
disp (43/600)	-0			/* IYI */

** Without Threshold (Scenario 6)
* Without threshold & with 550 MPs
v2seats party if old==1, party(par) formula(dhondt) district(dist) size(Total_MP)
disp (295/600)	-(280/550)	/* AKP */
disp (49/600)	-(46/550)	/* MHP */
disp (67/600)	-(69/550)	/* HDP */
disp (146/600)	-(123/550)	/* CHP */
disp (43/600)	-(32/550)	/* IYI */

* Without threshold & with 600 MPs (Scenario 2)
v2seats party if new==1, party(par) formula(dhondt) district(dist) size(New_MP)
disp (295/600)	-(306/600)	/* AKP */
disp (49/600)	-(52/600)	/* MHP */
disp (67/600)	-(72/600)	/* HDP */
disp (146/600)	-(133/600)	/* CHP */
disp (43/600)	-(37/600)	/* IYI */

** With Alliances
** 550 MPs (Scenarios 5 & 4) 
use "June2018.dta", clear
gen old=1					/* Ankara 2 districts, Bursa a single district */
replace old=0 if _n==6 | _n>=9 & _n<=11 | _n>=22 & _n<=23
gen new=1					/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

gen coal1=AKPARTİ+MHP
gen coal2=CHP+SP+IYI
rename (AKPARTİ MHP HDP CHP SP IYI Other coal1 coal2) (party#), addnumber

reshape long party, i(district) j(par)  
egen dist=group(district)

preserve
keep if inlist(par, 3, 7, 8, 9)
v2seats party if old==1, party(par) formula(dhondt) district(dist) athres(10) size(Total_MP) save(coalold)
use "coalold", clear
decode distl1, gen(dist) 
decode party, gen(par)
destring dist par, replace
drop distl1 party
save "coalold", replace
restore

merge 1:1 dist par using "coalold", gen(merge1)

gen cumhur=.a
gen millet=.a
capture noisily forval i=1/91{
qui sum dhondt if par==8 & dist==`i' 
qui replace cumhur=(r(mean)) if dist==`i'
qui sum dhondt if par==9 & dist==`i' 
qui replace millet=(r(mean)) if dist==`i'
}

preserve
keep if inlist(par, 1, 2)
recode cumhur (0=.)
v2seats party if old==1, party(par) formula(dhondt) district(dist) size(cumhur) save("coalold_v3")
restore

disp 319/550
disp (295/600)	-(276/550)	/* AKP */
disp (49/600)	-(43/550)	/* MHP */

preserve
keep if inlist(par, 4, 5, 6)
recode millet (0=.)
v2seats party if old==1, party(par) formula(dhondt) district(dist) size(millet) save("coalold_v4")
restore

disp (169/550)
disp (146/600)	-(136/550)	/* CHP */
disp (43/600)	-(33/550)	/* IYI */

** 600 MPs (Scenario 1 & Actual)
clear all
use "June2018.dta", clear
gen old=1					/* Ankara 2 districts, Bursa a single district */
replace old=0 if _n==6 | _n>=9 & _n<=11 | _n>=22 & _n<=23
gen new=1					/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

gen coal1=AKPARTİ+MHP
gen coal2=CHP+SP+IYI
rename (AKPARTİ MHP HDP CHP SP IYI Other coal1 coal2) (party#), addnumber

reshape long party, i(district) j(par)  
egen dist=group(district)

preserve
keep if inlist(par, 3, 7, 8, 9)
v2seats party if new==1, party(par) formula(dhondt) district(dist) athres(10) size(New_MP) save("coalnew")
use "coalnew", clear
decode distl1, gen(dist) 
decode party, gen(par)
destring dist par, replace
drop distl1 party
save "coalnew", replace
restore

merge 1:1 dist par using "coalnew", gen(merge1)

gen cumhur=.a
gen millet=.a
capture noisily forval i=1/91{
qui sum dhondt if par==8 & dist==`i' 
qui replace cumhur=(r(mean)) if dist==`i'
qui sum dhondt if par==9 & dist==`i' 
qui replace millet=(r(mean)) if dist==`i'
}

preserve
keep if inlist(par, 1, 2)
recode cumhur (0=.)
v2seats party if new==1, party(par) formula(dhondt) district(dist) size(cumhur)
restore

disp 344/600
disp (295/600)	-(276/550)	/* AKP */
disp (49/600)	-(43/550)	/* MHP */

preserve
keep if inlist(par, 4, 5, 6)
recode millet (0=.)
v2seats party if new==1, party(par) formula(dhondt) district(dist) size(millet)
restore

disp 189/600
disp (146/600)	-(136/550)	/* CHP */
disp (43/600)	-(33/550)	/* IYI */
